<?php
$localHost = array('localhost','127.0.0.1');

	define('HOST','localhost');
	define('DBNAME','photodigme');
	define('USER','root');
	define('PASSWORD','');
/*
if(in_array($_SERVER['HTTP_HOST'], $localHost)
{
	define('HOST','localhost');
	define('DBNAME','photodigme');
	define('USER','root');
	define('PASSWORD','');
}else{
	define('HOST','mysql13.000webhost.com');
	define('DBNAME','a8259506_photodi');
	define('USER','a8259506_fenwick');
	define('PASSWORD','hetre2012');
}*/

require_once "IDataTable.php";

abstract class AbstractDataTable implements IDataTable
{
	private $connectionHandler;
	
	protected $tableName;
	protected $primaryKey;
	protected $columnList;
	
	public function __construct()
	{
		$this->connectionHandler = new mysqli(HOST, USER, PASSWORD, DBNAME);
		return;
	}
	
	public function __destruct()
	{
		$this->connectionHandler->close();
		return;
	}

//INSERT
	public function insert($parameters)
	{
		if(is_array($parameters)){
			$strField = implode("','",$parameters);
			$strField = "'".$strField."'";
			$strField= str_replace("''","NULL",$strField);
		}else
		{
			$strField = $parameters;
			$strField = "'".$strField."'";
		}
		$request = "INSERT INTO ". $this->tableName ." VALUES (NULL ,".$strField.")";

		$response = $this->connectionHandler->query($request);

		if($response == true)
		{		
			return $this->connectionHandler->insert_id;
		}else{
			return false;
		}
	}

//SELECT
	public function selectFromPrimaryKey($primaryKeyValue)
	{
		$resultArray = null;
		
		$request = "SELECT * from ". $this->tableName ." WHERE ".
						$this->primaryKey ."=".$primaryKeyValue;
		$result = $this->connectionHandler->query($request);
		$row = $result->fetch_assoc();
		$result->close();
		return $row;
	}
	
	public function selectAll()
	{
		$resultArray = null;
		
		$request = "SELECT * from ". $this->tableName;
		$result = $this->connectionHandler->query($request);
		if ($result !== false){
			while($row = $result->fetch_assoc())
			{	
				$resultArray[$row[$this->primaryKey]]= $row;
			}
			$result->close();
		}
		return $resultArray;
	}
	
	public function select($fieldName ,$fieldValue )
	{
		$resultArray = null;
		
		$request = "SELECT * from ". $this->tableName ." WHERE ".
						$fieldName ."='".$fieldValue."'";
		$result = $this->connectionHandler->query($request);
		if (is_null($result) !== true){
			while($row = $result->fetch_assoc())
			{
				$resultArray[$row[$this->primaryKey]]= $row;
			}
		}else{
				$resultArray = false;
		}
		$result->close();
		return $resultArray;
		
	}

//UPDATE
	public function updateFromPrimaryKey($primaryKeyValue, $newFields)
	{
		$numberOfParameters = count($this->columnList);
		$numberOfValues = count($newFields);
		
		if($numberOfParameters != $numberOfValues){return false;}
		$i = 0;
		
		$request = "UPDATE ";
		$request .= $this->tableName;
		$request .= " SET ";
		do
		{	
			if($i != 0){ $request .= ", "; }
			$request .= $this->columnList[$i];
			$request .= "=";
			$request .= "'".$newFields[$i]."'";
			$i++;
		}while($i < $numberOfParameters);
		
		$request .= " WHERE ";
		$request .= $this->primaryKey;
		$request .= "=";
		$request .= "'".$primaryKeyValue."'";
		
		$this->connectionHandler->query($request);
	}
	
	//UPDATE
	public function updateFromPrimaryKeyChangeFields($primaryKeyValue,$newFieldToUpdate, $newFieldsValues)
	{
		$numberOfParameters = count($newFieldToUpdate);
		$numberOfValues = count($newFieldsValues);
		
		if($numberOfParameters != $numberOfValues){return false;}
		$i = 0;
		
		$request = "UPDATE ";
		$request .= $this->tableName;
		$request .= " SET ";
		do
		{	
			if($i != 0){ $request .= ", "; }
			$request .= $newFieldToUpdate[$i];
			$request .= "=";
			$request .= "'".$newFieldsValues[$i]."'";
			$i++;
		}while($i < $numberOfParameters);
		
		$request .= " WHERE ";
		$request .= $this->primaryKey;
		$request .= "=";
		$request .= "'".$primaryKeyValue."'";
		
		
		$this->connectionHandler->query($request);
	}
	
	public function update($fieldNames,$fieldValues, $newFields){}

//DELETE
	public function deleteFromPrimaryKey($primaryKeyValue){}
	public function delete($fieldNames,$fieldValues){}



}